<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Movie Player</title>

  <style type="text/css">
  
  body{margin:0;padding:0;font-family:'微软雅黑';background: black;}
  a{text-decoration:none; color: #ddd;}
  a:focus {outline: none;}
  

  </style>

</head>
<body style="">
</body>
</html>


<script type="text/javascript">



function VodPlayer(  ) {
  this.oninit = function(){}

  this.onError = function(){}
  this.onCompletion = function() {}

  this.player = null;
  this.isPaused = false;

  this.init = function() {
    if( this.player != null )  {
      return this.oninit();
    }
    if ( typeof(wae) == "undefined" ) {
      return ! setTimeout( this.init.bind(this) , 1000 );
    }

    this.player = wae.create("Media.VideoPlayer");
    this.player.setOnErrorListener();
    this.player.BufferSwitch(2);

    this.player.onError = (function(e) {
      console.log( "play error  "  );
      this.stopCheckTimer();
      this.onError();
    }).bind(this);

    this.player.onCompletion = (function(e) {
      this.hide_controler(); 
      this.stopCheckTimer();
      this.sendMessage("Completion","");
      this.onCompletion();
    }).bind(this);

    this.player.onSeekCompletion = (function(e){
          this.player.start();
          this.isPaused = false;     
    }).bind(this);

    this.player.onBufferingUpdate = this.onBufferingUpdate.bind(this);

    this.player.onPrepared = (function() {
      console.log("onPrepared, open the controller page ");
      this.player.start();
      this.isPaused = false;

      this.show_controller();

      // console.log("set title very quickly");
      this.sendMessage( "SetTitle", this.title );

      var u = this.murl;
      var ptime = parseInt( localStorage.getItem("player_" + encodeURIComponent(u) ) );

      if ( isNaN(ptime) ) {
        return;
      }
      var r = confirm("Resume last playing?");
      if ( ! r ) {
        return;
      }
      console.log("seek to playtime: " + ptime );
      this.player.seekTo( ptime );
    }).bind(this);

    this.oninit();
  }

  this.onBufferingUpdate = function( e ) {
    //console.log( "onBufferingUpdate : " + JSON.stringify(e));
    var status = e.arguments[2];
    var rate =  parseInt(e.arguments[1] );

    //console.log( "onBufferingUpdate : " + status + " : " + rate );

    if ( status == "MEDIA_INFO_BUFFERING_END" || rate == 100 ) {
      this.sendMessage( "Buffer", 100 );
    }
    else if ( status == "MEDIA_INFO_BUFFERING_START") {
      this.sendMessage( "Buffer", rate );
    }else {
      this.sendMessage("Buffer", rate );
    }
  }

  this.messgerObj = null;
  this.sendMessage = function( eventname, argument ) {
    if( ! this.messgerObj ) {
      this.messgerObj = wae.create("WAE.WindowMessager");
      this.messgerObj.setName( "main" );
      console.log("init message obj!");

      this.lasttime = 0;
      this.messgerObj.onplaypause = (function() {
        console.log("get playpause message!");
        var dateobj = new Date();
        var t = dateobj.getTime() - this.lasttime; 
        this.lasttime = dateobj.getTime();
        if ( t < 100 ) {
          console.log("found repeat message: " + t);
          return;
        }
        if( this.isPaused == false )
          this.player.pause();
        else
          this.player.start();
        this.isPaused = ! this.isPaused;
      }).bind(this);

      this.messgerObj.onpause = (function() {
        this.player.pause();
        this.isPaused = true;
      }).bind(this);

      this.messgerObj.onseek = (function(e) {
        var npos = parseInt(e.arguments[0]);
        console.log("recv seek ..." + npos);

        this.player.seekTo( npos );
      }).bind(this);

      this.messgerObj.onhide = (function() {
        console.log("get hide commond!");
        this.hide_controler();
      }).bind(this);

      this.messgerObj.onstop = (function() {
        this.hide_controler();
        this.onControllerClosed();

        this.player.stop();
        // history.back();
        var app = wae.create("Application");
        app.finishActivity();
      }).bind(this);


      this.messgerObj.onEpsiode = (function(e){
        console.log("get Epsiode message " + e.arguments[0]);
        var idx = parseInt( e.arguments[0] );

        this.playEpsiode( idx );
      }).bind(this);
      setTimeout( (function() {
        this.sendMessage(eventname, argument );
      }).bind(this), 1000 );
      return;
    }

    this.messgerObj.sendMessage("controller", eventname, argument );
  }

  this.onControllerClosed = function() {}
  this.controller = null;
  this.show_controller = (function() {
    // console.log("show_controller ... ");
    if ( ! this.controller ) {
    	console.log("show_controller ... create controller ");
      var fwin = wae.create("UI.WaePopupWebView");
      fwin.createWebView("controller", "vodcontroller.html", 0, 0, 1920, 1080 );
      fwin.onDestory = (function() {
        this.hide_controler();
        this.onControllerClosed();

        this.player.stop();
        // history.back();
        var app = wae.create("Application");
        app.finishActivity();
        // todo
      }).bind(this);

      fwin.onCreated = function() {
        this.showWebView( "controller", true);
      }
      fwin.loadUrl("controller", "vodcontroller.html" );
      this.controller = fwin;
    }

    var fwin = this.controller ;
    //console.log("show_controller ... show it ");
    fwin.showWebView( "controller", true);

    this.startCheckTimer();      
  }).bind(this);


  this._check_timer = null;
  this.stopCheckTimer = function( ) {
    console.log("stopCheckTimer ... ");
    if ( this._check_timer != null )
      clearInterval( this._check_timer );
    this._check_timer = null;
  }
  this.startCheckTimer = function() {
    this.stopCheckTimer();

    // console.log("startCheckTimer ... ");
    this._chkfunc = function() {
      if ( this._check_timer == null ) return;
      var info = {};
      var duration = this.player.getDuration();
      var playtime = this.player.getCurrentPosition();
      info.duration = duration;
      info.playtime = playtime;

      this.playtime = playtime;
      // console.log("startCheckTimer ... update playtime: " + playtime );
      this.sendMessage( "Update", JSON.stringify(info) );
    }
    this._check_timer = setInterval( this._chkfunc.bind(this) , 1000);
    this._chkfunc();
  }

  this.hide_controler = function() {
    this.stopCheckTimer();

    var fwin = this.controller;
    console.log("fwin is " + fwin );
    if( !! fwin ) {
      console.log("fwin is hideWebView "  );
      fwin.hideWebView("controller");
    } 
  }

  this.start = function( url, title ) {
    if ( !url ) url = this.murl;
    else this.murl = url;

    if( this.player == null ) {
      setTimeout( this.start.bind(this), 1000 );      
      return false;
    }

    this.player.setFullScreen();
    this.player.setDataSource( url );
    // this.player.setDataSource( this.getSecUrl(  url ) );

    if ( !! title ) {
      this.title = title;
      setTimeout( (function(){
        this.sendMessage( "SetTitle", title );
      }).bind(this), 1000 * 3 );
    }
  }

  this.init();
}

  
</script>
<script type="text/javascript">

var close_player = false;
function closePlayer() {
  close_player = true;
  var app = wae.create("Application");
  app.finishActivity();
}

var drmobj = null;
var vodPlayer = new VodPlayer();
vodPlayer.oninit = function() {

  drmobj = wae.create("UnitedDRM");
  if ( !! drmobj ) {
    if ( drmobj.hasInit() && drmobj.hasLogin() ) {
      // logined
    } else {
      location.href = "drmlogin.html";  
      return;    
    }
  }

  var param = wae.create("DATA.Parameters");

  var title = param.get("title");
  var path  = param.get("URLS");
  if ( !path ) path = param.get("urls");
  if ( !path ) path = param.get("path");

  if ( !path ) {
    try {
      path = param.getData();
    }catch(e){}
  }

  if ( ! path ) {
    alert("Fatal: no media!");
    var app = wae.create("Application");
    app.finishActivity();
    return;
  }

  // alert( "new 2 title: "+ title + "\n" + path );

  if ( !! drmobj )
    path = drmobj.getUrl( path );

  this.show_controller();
  this.start( path, title );  
}

vodPlayer.onError = function() {
  closePlayer();
}
vodPlayer.onCompletion = function() {
  var u = this.murl;
  localStorage.removeItem("player_" + encodeURIComponent(u) );
  closePlayer();
}

</script>

<script type="text/javascript">

setTimeout( function() {
  wae.onPause = function() {
    if ( close_player == true ) {
      console.log("playing over");
      return;
    }
    var u  = vodPlayer.murl ;
    if ( ! u ) {
      console.log("no url , playing not started!" );
      return;
    }

    var ptime = vodPlayer.playtime;
    console.log("save play position " + ptime + " for " + u );
    localStorage.setItem("player_" + encodeURIComponent(u) , ptime );
  }
}, 1000 * 3 );

</script>

